<?php
/**
 * QrCodesController.php
 *
 * @copyright   2023 thingsworld.com.cn - All Rights Reserved
 * @link        http://thingsworld.com.cn/
 * @author      stiffer.chen <chenl@thingsworld.com.cn>
 * @created     2023-02-14 02:07:30
 * @modified    2023-02-14 02:07:30
 *
 */

namespace App\Http\Controllers;

use App\Repositories\Product\QrCodeRepo;
use Illuminate\Http\Request;

class QrCodesController extends BaseController
{
    /**
     * @OA\Get(
     *     path="/qr_code",
     *     tags={"QrCode"},
     *     summary="获取二维码列表",
     *     description="获取二维码列表",
     *     operationId="qrcode_index",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="product_id", in="query", description="商品ID", required=false, @OA\Schema(default=0, type="integer")),
     *     @OA\Parameter(name="product_name", in="query", description="商品名称", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="code", in="query", description="二维码编号", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="page", in="query", description="当前页数", required=false, @OA\Schema(default="", type="integer")),
     *     @OA\Parameter(name="limit", in="query", description="每页数量(默认10)", required=false, @OA\Schema(default=10,type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function index(Request $request)
    {
        $qrCodes = QrCodeRepo::getList();
        return list_message("获取成功", true, $qrCodes);
    }

    /**
     * @OA\Get(
     *     path="/qr_code_search",
     *     tags={"QrCode"},
     *     summary="搜索二维码",
     *     description="搜索二维码",
     *     operationId="qrcode_search",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="code", in="query", description="二维码code", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="filter_binded", in="query", description="是否过滤已绑定产品的二维码(0、1), 1表示过滤", required=false, @OA\Schema(default=1,type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function search(Request $request)
    {
        try {
            $qrcodes = QrCodeRepo::search();
            return message("获取成功", true, $qrcodes);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Get(
     *     path="/qr_code/{id}",
     *     tags={"QrCode"},
     *     summary="获取二维码详情",
     *     description="获取二维码详情",
     *     operationId="qrcode_show",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id",in="path",description="二维码ID",required=true,@OA\Schema(default="",type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function show(Request $request, $id)
    {
        try {
            $qrcode = QrCodeRepo::fetch($id);
            return message("获取成功", true, $qrcode);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Post(
     *     path="/qr_code",
     *     tags={"QrCode"},
     *     summary="添加二维码",
     *     description="添加二维码",
     *     operationId="qrcode_store",
     *     security={{"bearerAuth":{}}},
     *     @OA\RequestBody(
     *          required=true,
     *          @OA\JsonContent(
     *              type="object",
     *              required={"product_id"},
     *              @OA\Property(property="product_id", type="integer", example=0, description="商品ID"),
     *          )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function store(Request $request)
    {
        try {
            $qrcode = QrCodeRepo::store(0);
            return message("创建成功", true, $qrcode);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Post(
     *     path="/qr_code_generate",
     *     tags={"QrCode"},
     *     summary="批量生成二维码",
     *     description="批量生成二维码",
     *     operationId="qrcode_generate",
     *     security={{"bearerAuth":{}}},
     *     @OA\RequestBody(
     *         description="表单数据",
     *         @OA\MediaType(
     *             mediaType="application/x-www-form-urlencoded",
     *             @OA\Schema(
     *                 type="object",
     *                 required={"count"},
     *                 @OA\Property(property="count", description="二维码数量", type="integer")
     *             )
     *         )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function generate(Request $request)
    {
        try {
            $count = intval($request->get('count'));
            QrCodeRepo::generate($count);
            return message('生成成功', true);
        }  catch (\Exception $e) {
            return message('生成失败', false);
        }
    }

    /**
     * @OA\Put(
     *     path="/qr_code/{id}",
     *     tags={"QrCode"},
     *     summary="修改二维码",
     *     description="修改二维码",
     *     operationId="qrcode_update",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id",in="path",description="二维码ID",required=true,@OA\Schema(default="",type="integer")),
     *     @OA\RequestBody(
     *          required=true,
     *          @OA\JsonContent(
     *              type="object",
     *              required={"product_id"},
     *              @OA\Property(property="product_id", type="integer", example=0, description="商品ID"),
     *          )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function update(Request $request, $id)
    {
        try {
            $qrcode = QrCodeRepo::store($id);
            return message("更新成功", true, $qrcode);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Get(
     *     path="/qr_code_export",
     *     tags={"QrCode"},
     *     summary="导出二维码",
     *     description="导出二维码",
     *     operationId="qrcode_export",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="ids", in="query", description="要导出的二维码ID，用半角逗号分隔(1,2,3,4,5)", required=true, @OA\Schema(default="", type="string")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function export(Request $request)
    {
        try {
            $url = QrCodeRepo::export_special();
            return message("导出成功", true, ['url' => $url]);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Delete(
     *     path="/qr_code/{id}",
     *     tags={"QrCode"},
     *     summary="删除二维码",
     *     description="删除二维码",
     *     operationId="qrcode_destroy",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id",in="path",description="二维码ID",required=true,@OA\Schema(default="",type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function destroy(Request $request, $id)
    {
        try {
            QrCodeRepo::destroy($id);
            return message("删除成功", true);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }
}